package com.hnucm.house.base;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity<V,P extends BasePresenter<V>> extends AppCompatActivity {
    protected P mPresenter;

    //使用ButterKnife
    protected Unbinder mUnbinder;

    //获取布局id
    protected abstract int getContentViewId();

    //初始化
    protected abstract void init(Bundle savedInstanceState);

    //创建Presenter
    protected abstract P createPresenter();

    //适配屏幕
    private static float sNocompatDensity;
    private static float sNocompatScaleDensity;

    //p层持有v的引用，调用init
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCustomDesity(this,getApplication());
        setContentView(getContentViewId());
        mUnbinder = ButterKnife.bind(this);
        mPresenter = createPresenter();
        if(mPresenter != null){
            mPresenter.attchView((V) this);
        }
        //此处调用init
        init(savedInstanceState);
    }

    private static void setCustomDesity(@NonNull Activity activity, @NonNull final Application application){
        final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();

        if(sNocompatDensity == 0){
            sNocompatDensity = appDisplayMetrics.density;
            sNocompatScaleDensity = appDisplayMetrics.scaledDensity;
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged( Configuration newConfig) {
                    if(newConfig != null && newConfig.fontScale > 0){
                        sNocompatScaleDensity = application.getResources().getDisplayMetrics()
                                .scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {

                }
            });
        }
        final float targetDensity = appDisplayMetrics.widthPixels / 360f;
        final float targetScaleDensity = targetDensity * (sNocompatScaleDensity / sNocompatDensity);
        final int targetDensityDpi = (int) (160 * targetDensity);

        appDisplayMetrics.density = targetDensity;
        appDisplayMetrics.scaledDensity = targetScaleDensity;
        appDisplayMetrics.densityDpi = targetDensityDpi;

        final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density = targetDensity;
        activityDisplayMetrics.scaledDensity = targetScaleDensity;
        activityDisplayMetrics.densityDpi = targetDensityDpi;
    }

    //释放p层对v的引用
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        mPresenter.detachView();
    }
}
